﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PeachLite.Helpers;
using System.IO;
using System.Text;

namespace PeachLite.Admin
{
    public partial class FileManger : PageBase
    {
        /// <summary>
        /// 文件/文件夹列表模板
        /// </summary>
        protected static string LIST_FORMAT = "<li><dl><dt p='{4}'><a title='{3}' {5}><img src='{0}' alt='{2}' /></a><span class='del' onclick='del(this);'>×</span></dt><dd>{1}</dd></dl></li>";

        protected override void Do()
        {
            base.Do();

            //获得请求文件夹路径
            string path = Request.QueryString["p"] == null ? "" : Request.QueryString["p"].Trim();
            path = path.Replace("\\", "/");

            //检核文件夹路径
            if (path == "" | !path.StartsWith(ConfigHelper.Config.DefaultPath))
            {
                path = ConfigHelper.Config.DefaultPath;
            }
            try
            {
                filePath.Text = path;
                backPath.Text = FileHelper.GetFatherDirectoryPath(path);

                //获得指定目录下的文件和文件夹列表
                DataTable tab = FileHelper.GetFile(Server.MapPath(path));
                StringBuilder sb = new StringBuilder(string.Empty);
                for (int i = 0; i < tab.Rows.Count; i++)
                {
                    string name = tab.Rows[i]["Name"].ToString();
                    string href = "";   //链接
                    string p = path.EndsWith("/") ? path + name : path + "/" + name;

                    //获得图标
                    string icon = FileHelper.GetIcon(Server.MapPath(p), p);

                    if (tab.Rows[i]["Type"].ToString() == FileHelper.FOLDER)
                    {
                        href = "href='?p=" + p + "'";
                    }
                    sb.Append(string.Format(LIST_FORMAT, icon, name, name, name, p, href));
                }
                fileList.Text = sb.ToString();
            }
            catch (Exception ex)
            {
                ShowMessage(0, ex.Message, ex.Message, null);
            }
        }
    }
}